/**
 *
 */
package com.gxa.ssm251.service.impl;


import com.gxa.ssm251.exception.BusinessException;
import com.gxa.ssm251.mapper.AdminMapper;
import com.gxa.ssm251.pojo.Admin;
import com.gxa.ssm251.pojo.City;
import com.gxa.ssm251.service.AdminService;
import com.gxa.ssm251.util.MD5Util;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author bilei
 * @date 2021年3月31日
 */
@Controller("adminService")
public class AdminServiceImpl implements AdminService {


    @Resource
    private AdminMapper adminMapper = null;

    @Override
    public Admin login(String name, String pwd) throws Exception {
        Admin loadmin = adminMapper.selectByName(name);

        if (loadmin != null) {
            if (MD5Util.encode(pwd+loadmin.getSalt()).equals(loadmin.getPwd())) {
                if (loadmin.getInUse()) {
                    return loadmin;
                } else {
                    throw new BusinessException("该用户已经被禁用,为不可用状态");
                }

            } else {
                throw new BusinessException("用户名或密码不正确");
            }
        } else {
            throw new BusinessException("不存在的用户");
        }


//        Admin loAdmin = adminMapper.selectByName(phone);
////		System.out.println("service的"+loAdmin);
//        if (loAdmin != null) {
//
//            if (MD5Util.encode(pwd).equals(loAdmin.getPwd())) {
//                return loAdmin;
//            } else {
//                throw new BusinessException("用户名或密码不正确");
//                return null;
//            }
//        } else {
//            throw new BusinessException("不存在的用户");
//			return null;
//        }


    }

    @Override
    public Integer setTime(Admin admin) {
        Integer integer = adminMapper.updateTime(admin);
        return integer;
    }

    @Override
    public Integer addAdmin(Admin admin) throws Exception {
        Admin admin1 = adminMapper.selectByName(admin.getName());
        Integer integer=0;
        if (admin1!=null){
            throw new BusinessException("用户名已存在");
        }else {
             integer = adminMapper.insertAdmin(admin);
            return integer;
        }


    }

    @Override
    public Integer deleteAdmin(Integer id) {
        Integer integer = adminMapper.deleteByAdminId(id);
        return integer;
    }

    @Override
    public List<Admin> selectAllAdmin() {
        List<Admin> admins = adminMapper.selectAll();
        return admins;
    }

    @Override
    public Integer upStatus(Admin admin) {
        Admin admin1 = adminMapper.selectById(admin.getId());
        if (admin1.getInUse()) {
            admin1.setInUse(false);
        } else {
            admin1.setInUse(true);
        }
        Integer integer = adminMapper.updateStatus(admin1);
        return integer;
    }

    @Override
    public Integer upAdmin(Admin admin) {
        Integer integer = adminMapper.updateAdmin(admin);
        return integer;
    }

    @Override
    public Admin selecAdmintByName(String name) {
        Admin loadmin = adminMapper.selectByName(name);
        return loadmin;
    }

    @Override
    public Integer addLimit(Integer id, List<String> list) {
        Integer integer = 0;
        Integer lines = 0;
        for (String s : list) {
            integer += adminMapper.insertAdminLimit(id, Integer.valueOf(s));
        }
        if (integer == list.size()) {
            lines = 1;
        }

        return lines;
    }

    @Override
    public List selectAllLimits(Integer id) {
       List Limits= adminMapper.selectLimits(id);
        return Limits;
    }

    @Override
    public List<City> selectAllCity() {
       List citys= adminMapper.selectAllCity();
        return citys;
    }

    @Override
    public Integer deleteCity(Integer id) {
       Integer integer= adminMapper.deleteByCityId(id);
        return integer;
    }

    @Override
    public Integer addCity(City city) throws Exception {
       City city1=adminMapper.selectByCityName(city.getName());
        Integer integer=0;
       if (city1!= null){
           throw new BusinessException("该城市已存在,请重新添加");
       }else {
           integer= adminMapper.insertCity(city);
       }

        return integer;
    }


}
